 <?php
include("include/config.inc.php");
include('thumbnailClass.php');
if(!isset($_SESSION['s_activIds']))
{
  header("Location:checkLogin.php");
}
else
{
  $userId = ($_SESSION['s_activIds']);
  
  //Update Image
  if(isset($_REQUEST['submit']))
  {
    $contractId     = $_POST['contractId'];
    $invoiceId     = $_POST['invoiceId'];
    
    $filename  = $_FILES['uploadFile']['name'];
    $file = "uploadDoc/".$filename;
    move_uploaded_file($_FILES['uploadFile']['tmp_name'], $file); 
    $updateImage = "UPDATE userdoc
                       SET uploadFile = '".$filename."'
                     WHERE contractId = '".$contractId."'
                       AND invoiceId = ".$invoiceId;
    $updateImageRes = mysql_query($updateImage);
  }
  
  $o=0;
  $contractArray = array();
  $selectContract = "SELECT contractId,contractNo
                       FROM contract";
  $selectContractRes = mysql_query($selectContract);
  while($contractRow = mysql_fetch_array($selectContractRes))
  {
    $contractArray['contractId'][$o] = $contractRow['contractId'];
    $contractArray['contractNo'][$o] = $contractRow['contractNo'];
    $o++;
  }
	
  $l=0;
  $invoiceArray = array();
  $selectInvoice = "SELECT invoiceId,invoiceNo
                      FROM invoice";
  $selectInvoiceRes = mysql_query($selectInvoice);
  while($invoiceRow = mysql_fetch_array($selectInvoiceRes))
  {
    $invoiceArray['invoiceId'][$l] = $invoiceRow['invoiceId'];
    $invoiceArray['invoiceNo'][$l] = $invoiceRow['invoiceNo'];
    $l++;
  }
  
  $t=0;
  $detailArray = array();
  $selectDetail = "SELECT userdoc.uploadFile,DATE_FORMAT(userdoc.uploadDateTime,'%d-%m-%Y:%h%i')AS uploadDateTime,contract.contractNo,invoice.invoiceNo,user.company
                     FROM userdoc
                     LEFT JOIN contract ON contract.contractId = userdoc.contractId
                     LEFT JOIN invoice ON invoice.invoiceId = userdoc.invoiceId
                     LEFT JOIN user ON user.userId = userdoc.uploadedBy
                    WHERE userdoc.userId = ".$userId;
  $selectDetailRes = mysql_query($selectDetail);
  while($dtlRow = mysql_fetch_array($selectDetailRes))
  {
    $detailArray[$t]['contractNo']     = $dtlRow['contractNo'];
    $detailArray[$t]['invoiceNo']      = $dtlRow['invoiceNo'];
    $detailArray[$t]['company']        = $dtlRow['company'];
    $detailArray[$t]['uploadFile']     = $dtlRow['uploadFile'];
    $detailArray[$t]['uploadDateTime'] = $dtlRow['uploadDateTime'];
    $t++;
  }
  
  include("bottom.php");
  $smarty->assign("detailArray",$detailArray);
  $smarty->assign("contractArray",$contractArray);
  $smarty->assign("invoiceArray",$invoiceArray);
  $smarty->assign("userId",$userId);
  $smarty->display("myDoc.tpl");
}
?>